# 作者：20242321汤泽鹏
# 地点：电科院
# 时间：4 a.m
# 代理地址：Los Angels

from cryptography.fernet import Fernet
import socket

with open("secret.key", "rb") as key_file:
    key = key_file.read()
cipher = Fernet(key)

def start_sever():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('192.168.43.134', 8080))
    server_socket.listen(5)
    print("服务器已启动，等待连接...")
    while True:
        client_socket,client_address = server_socket.accept()
        print("收到来自{client_address}的连接".format(client_address=client_address))
        encrypted_data = client_socket.recv(1024)
        decrypted_data = cipher.decrypt(encrypted_data)
        with open("received_file.txt","wb") as receive_file:
            receive_file.write(decrypted_data)
        print("文件接收并解密完成")
        print(f"客户端说:{decrypted_data.decode("UTF-8")}")

        client_socket.close()

start_sever()